<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

    <style type="text/css">
        #div1,#div2,#div3,#div4 {width:198px; height:66px;padding:10px;border:1px solid #aaaaaa;}
    </style>
    <script type="text/javascript">

        //拖动什么
        function drag(ev)
        {
            ev.dataTransfer.setData("Text",ev.target.id);
        }

        //放到何处
        function allowDrop(ev)
        {
            ev.preventDefault();
        }

        //进行放置
        function drop(ev)
        {
            ev.preventDefault();
            var data=ev.dataTransfer.getData("Text");
            ev.target.appendChild(document.getElementById(data));
        }
    </script>
</head>
<body>

<p>请把 W3School 的图片拖放到矩形中：</p>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">可以放在这</div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)">也可以放在这</div>
<br />
 首先，为了使元素可拖动，把 draggable 属性设置为 true
<br>
<img id="drag2" src="eg_dragdrop_w3school.gif" draggable="true" ondragstart="drag(event)" />

<br>
<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div4" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<img src="eg_dragdrop_w3school.gif" draggable="true" ondragstart="drag(event)" id="drag1" />

</body>
</html>
